home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
src
/
fig08_08.jar
/
Ch08
/
Fig08_08
/
Hugeint1.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1997-10-25
|
2KB
|
76 lines
// Fig. 8.8: hugeint1.cpp
// Member and friend function definitions for class HugeInt
#include <string.h>
#include "hugeint1.h"
// Conversion constructor
HugeInt::HugeInt( long val )
{
int i;
for ( i = 0; i <= 29; i++ )
integer[ i ] = 0; // initialize array to zero
for ( i = 29; val != 0 && i >= 0; i-- ) {
integer[ i ] = val % 10;
val /= 10;
}
}
HugeInt::HugeInt( const char *string )
{
int i, j;
for ( i = 0; i <= 29; i++ )
integer[ i ] = 0;
for ( i = 30 - strlen( string ), j = 0; i <= 29; i++, j++ )
integer[ i ] = string[ j ] - '0';
}
// Addition
HugeInt HugeInt::operator+( HugeInt &op2 )
{
HugeInt temp;
int carry = 0;
for ( int i = 29; i >= 0; i-- ) {
temp.integer[ i ] = integer[ i ] +
op2.integer[ i ] + carry;
if ( temp.integer[ i ] > 9 ) {
temp.integer[ i ] %= 10;
carry = 1;
}
else
carry = 0;
}
return temp;
}
// Addition
HugeInt HugeInt::operator+( int op2 )
{ return *this + HugeInt( op2 ); }
// Addition
HugeInt HugeInt::operator+( const char *op2 )
{ return *this + HugeInt( op2 ); }
ostream& operator<<( ostream &output, HugeInt &num )
{
int i;
for ( i = 0; ( num.integer[ i ] == 0 ) && ( i <= 29 ); i++ )
; // skip leading zeros
if ( i == 30 )
output << 0;
else
for ( ; i <= 29; i++ )
output << num.integer[ i ];
return output;
}